import requests
import json
from openai import OpenAI

class WebApi:

    @staticmethod
    def get_data_today():
        """
        当前时间的信息
        :return:
        """
        url = 'https://api.t1qq.com/api/tool/day/time?key=E0km9QE18MtO9jUujJ002mGJW86'
        response = requests.get(url)
        return response.json()

    @staticmethod
    def get_data(y, m, d, h):
        """
        获取指定时间的信息
        :param y:year
        :param m:month
        :param d:day
        :param h:hour
        :return:info_s
        """
        # 设置url
        url = 'https://api.t1qq.com/api/tool/day/time?key=E0km9QE18MtO9jUujJ002mGJW86&y=%s&m=%s&d=%s&h=%s' % (
            y, m, d, h)
        # 发送post请求
        response = requests.get(url)

        # 获取响应内容
        return response.json()

    @staticmethod
    def get_info_s(r, now_hour, ba_zi):
        if ba_zi == "例如：巽卦挂义":
            info_s = "天干地支：%s%s%s%s,属相：%s,阴历时间：%s,具体时辰：%s,六壬月：%s,六壬天：%s,六壬日：%s," \
                     "月相：%s,物候：%s,节气：%s,三元九运：%s,日禄：%s,六爻：%s,七曜：%s,彭祖天干：%s,彭祖地支：%s,八字：%s," \
                     "纳音：%s,日宜：%s,日忌：%s,时宜：%s,时忌：%s,吉神：%s,凶神：%s,每日相冲生肖：%s,每日相冲方位：%s," \
                     "星宿：%s,星宿吉凶：%s,星宿歌曲：%s,阳贵神方位：%s,阴贵神方位：%s,喜神方位：%s,福神方位：%s,财神方位：%s," \
                     "月胎神：%s,今日胎神：%s,太岁方向：%s,十二值星：%s日,北斗九星-年份-九数：%s,北斗九星-年份-七色：%s,北斗九星-年份-五行属性：%s," \
                     "方位：%s,北斗九星：%s,北斗九星名称：%s,北斗九星-吉凶：%s,北斗九星-奇门：%s,北斗九星-八门：%s,北斗九星-八门吉凶：%s," \
                     "北斗九星-八门-奇门阴阳：%s,北斗九星-太乙名称：%s,北斗九星-太乙类型：%s,北斗九星-太乙九神歌诀：%s,北斗九星详解：%s" \
                     % \
                     (r[2], r[3], r[4], r[5], r[1], r[22], now_hour, r[18], r[19], r[20], r[31], r[32], r[33], r[37],
                      r[38], r[39], r[40], r[41], r[42], r[43], r[44], r[45], r[46], r[47], r[48], r[49], r[50], r[54],
                      r[55], r[58], r[59], r[60], r[62], r[64], r[66], r[68], r[70], r[71], r[72], r[78], r[79], r[80],
                      r[81], r[82], r[84], r[85], r[86], r[87], r[88], r[89], r[90], r[91], r[92], r[93], r[94], r[96]
                      )
            info_temp = "当前吉凶"
        else:
            info_s = "八字：%s,天干地支：%s%s%s%s,属相：%s,阴历时间：%s,具体时辰：%s,六壬月：%s,六壬天：%s,六壬日：%s," \
                     "月相：%s,物候：%s,节气：%s,三元九运：%s,日禄：%s,六爻：%s,七曜：%s,彭祖天干：%s,彭祖地支：%s,八字：%s," \
                     "纳音：%s,日宜：%s,日忌：%s,时宜：%s,时忌：%s,吉神：%s,凶神：%s,每日相冲生肖：%s,每日相冲方位：%s," \
                     "星宿：%s,星宿吉凶：%s,星宿歌曲：%s,阳贵神方位：%s,阴贵神方位：%s,喜神方位：%s,福神方位：%s,财神方位：%s," \
                     "月胎神：%s,今日胎神：%s,太岁方向：%s,十二值星：%s日,北斗九星-年份-九数：%s,北斗九星-年份-七色：%s,北斗九星-年份-五行属性：%s," \
                     "方位：%s,北斗九星：%s,北斗九星名称：%s,北斗九星-吉凶：%s,北斗九星-奇门：%s,北斗九星-八门：%s,北斗九星-八门吉凶：%s," \
                     "北斗九星-八门-奇门阴阳：%s,北斗九星-太乙名称：%s,北斗九星-太乙类型：%s,北斗九星-太乙九神歌诀：%s,北斗九星详解：%s" \
                     % \
                     (ba_zi, r[2], r[3], r[4], r[5], r[1], r[22], now_hour, r[18], r[19], r[20], r[31], r[32], r[33],
                      r[37],
                      r[38], r[39], r[40], r[41], r[42], r[43], r[44], r[45], r[46], r[47], r[48], r[49], r[50], r[54],
                      r[55], r[58], r[59], r[60], r[62], r[64], r[66], r[68], r[70], r[71], r[72], r[78], r[79], r[80],
                      r[81], r[82], r[84], r[85], r[86], r[87], r[88], r[89], r[90], r[91], r[92], r[93], r[94], r[96]
                      )
            info_temp = ba_zi
        info_s = info_s.replace(" ", "").replace("\n", "").replace("，", ",").replace("。", ".").replace("：", ":")
        return info_s

    @staticmethod
    def content_info(info_s):
        url = "https://qianfan.baidubce.com/v2/app/conversation/runs"
        print("AI解析中：", info_s)
        # 复杂AI
        # payload = json.dumps({
        #     "app_id": "0275a963-2c95-4ec1-9100-5116d2a1d6cc",
        #     "query": info_s,
        #     "stream": False,
        #     "conversation_id": "dfd3f6ac-de79-4359-85ba-99f1978b5e05"
        # })
        # 快速AI
        payload = json.dumps({
            "app_id": "6626c890-fa09-44c2-9a20-bf5cf9bc100b",
            "query": info_s,
            "stream": False,
            "conversation_id": "91afe999-68c0-46eb-b8d2-7e6560c30909"
        })
        headers = {
            'Content-Type': 'application/json',
            'X-Appbuilder-Authorization': 'Bearer bce-v3/ALTAK-3SGdRDCM1QArx5ETW11Np/2819c6639d83e0a2f4012ec7b9c2cc866e4170b8'
        }

        response = requests.request("POST", url, headers=headers, data=payload)

        lens = 0
        resu = ""

        for info in response.json()["content"]:
            try:
                if len(info['outputs']['text']) > lens:
                    resu = info['outputs']['text']
            except Exception as e1:
                pass
        data = resu.replace("```json", "").replace("```", "").replace(" ", "").replace("\n", "").replace("，",
                                                                                                         ",").replace(
            "。", ".").replace("：", ":")

        # result_sum = json.dumps({
        #     "result": data,
        #     "info_s": info_s_change
        # })
        return json.loads(data)

    @staticmethod
    def get_ai_info():
        url = "https://qianfan.baidubce.com/v2/app/conversation/runs"

        payload = json.dumps({
            "app_id": "bd59c750-3e23-4711-92cb-ff823078362b",
            "query": "现在出门",
            "stream": False,
            "conversation_id": "df5319df-1b8f-4c55-bb8a-0e09d63971b8"
        })
        headers = {
            'Content-Type': 'application/json',
            'X-Appbuilder-Authorization': 'Bearer bce-v3/ALTAK-3SGdRDCM1QArx5ETW11Np/2819c6639d83e0a2f4012ec7b9c2cc866e4170b8'
        }

        response = requests.request("POST", url, headers=headers, data=payload)
        lens = 0
        resu = ""
        for info in response.json()["content"]:
            try:
                if len(info['outputs']['text']) > lens:
                    resu = info['outputs']['text']
            except Exception as e1:
                pass
        print("时间："+resu)
        return resu

    @staticmethod
    def get_day_one_gua():
        # 设置url
        url = 'https://api.t1qq.com/api/tool/cg?key=E0km9QE18MtO9jUujJ002mGJW86'

        # 发送post请求
        response = requests.get(url)

        # 获取响应内容
        result = response.json()
        print(result['g'])
        # 返回
        return result['g'].replace(' ', '').replace(' ', '').replace('\n', '').replace('\r', '').replace('\t',
                                                                                                         '').replace(
            '\u3000', '').replace('，', '').replace('。', '.').replace('：', ':').replace('？', '?').replace('！',
                                                                                                         '!').replace(
            '；', ';').replace('（', '(').replace('）', ')').replace('《', '<').replace('》', '>').replace('【', '[')

    @staticmethod
    def get_ai_zhou_yi(q):
        url = "https://qianfan.baidubce.com/v2/app/conversation/runs"

        payload = json.dumps({
            "app_id": "2e6f2be0-6bc8-4c86-b7d5-a4d368886833",
            "query": q,
            "stream": False,
            "conversation_id": "换成自己的"
        })
        headers = {
            'Content-Type': 'application/json',
            'X-Appbuilder-Authorization': 'Bearer bce-v3/ALTAK-3SGdRDCM1QArx5ETW11Np/2819c6639d83e0a2f4012ec7b9c2cc866e4170b8'
        }

        response = requests.request("POST", url, headers=headers, data=payload)
        lens = 0
        resu = ""
        for info in response.json()["content"]:
            try:
                if len(info['outputs']['text']) > lens:
                    resu = info['outputs']['text']
            except Exception as e1:
                print("易经生活万事通信息异常：", e1)
        print("AI分析的：", resu)
        return resu